﻿<UserControl x:Class="TanmiaGrp.GeneralLedger.WinApp.Scenarios.JournalEditorView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:loc="clr-namespace:TanmiaGrp.GeneralLedger.WinApp"
             xmlns:Scenarios="clr-namespace:TanmiaGrp.GeneralLedger.WinApp.Scenarios"  
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mui="http://firstfloorsoftware.com/ModernUI"
             mc:Ignorable="d"
             d:DesignWidth="300" Height="500"
             FlowDirection="RightToLeft">
    <UserControl.Resources>

        <loc:NullToVisibilityConverter x:Key="nullConverter" />

        <ControlTemplate x:Key="ValidationTemplate">
            <StackPanel Orientation="Vertical">
                <Border BorderThickness="1"
                        BorderBrush="#FFdc000c"
                        VerticalAlignment="Top">
                    <Grid>
                        <AdornedElementPlaceholder x:Name="adorner"
                                                   Margin="-1" />
                    </Grid>
                </Border>
                <Border x:Name="errorBorder"
                        Background="#FFdc000c"
                        Opacity="0"
                        CornerRadius="0"
                        IsHitTestVisible="False"
                        MinHeight="20">
                    <TextBlock Text="{Binding ElementName=adorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
                               Foreground="White"
                               Margin="8,1,8,3"
                               TextWrapping="Wrap"
                               VerticalAlignment="Center" />
                </Border>
            </StackPanel>
            <ControlTemplate.Triggers>
                <DataTrigger Value="True">
                    <DataTrigger.Binding>
                        <Binding ElementName="adorner"
                                 Path="AdornedElement.IsKeyboardFocused" />
                    </DataTrigger.Binding>
                    <DataTrigger.EnterActions>
                        <BeginStoryboard x:Name="fadeInStoryboard">
                            <Storyboard>
                                <DoubleAnimation Duration="00:00:00.15"
                                                 Storyboard.TargetName="errorBorder"
                                                 Storyboard.TargetProperty="Opacity"
                                                 To="1" />
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                    <DataTrigger.ExitActions>
                        <StopStoryboard BeginStoryboardName="fadeInStoryboard" />
                        <BeginStoryboard x:Name="fadeOutStoryBoard">
                            <Storyboard>
                                <DoubleAnimation Duration="00:00:00"
                                                 Storyboard.TargetName="errorBorder"
                                                 Storyboard.TargetProperty="Opacity"
                                                 To="0" />
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.ExitActions>
                </DataTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

        <Style x:Key="ComboElementStyle"
               TargetType="ComboBox">
            <Setter Property="ItemsSource"
                    Value="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.TransactionalAccounts}" />
            <Setter Property="DisplayMemberPath"
                    Value="AccountName" />
            <Setter Property="SelectedValuePath"
                    Value="AccountID" />
        </Style>
    </UserControl.Resources>


    <Grid>
        <Grid.Resources>
            <Style TargetType="HeaderedContentControl">
                <Setter Property="Foreground" Value="#ff6e6e6e" />
                <Setter Property="Focusable" Value="False" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="HeaderedContentControl">
                            <StackPanel Margin="0,0,0,2">
                                <ContentPresenter ContentSource="Header" Margin="6,0,6,4" />
                                <ContentPresenter ContentSource="Content" Margin="7,4,7,4" />
                            </StackPanel>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Grid.Resources>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <!-- Header -->
        <DockPanel Grid.Row="0" Height="46">
            <Scenarios:WindowBar x:Name="windowBar" DockPanel.Dock="Left" PopouClicked="OnPopout" />
            <TextBlock Style="{StaticResource CaptionStyle}" 
                       Text="{Binding Path=StateText}" />
            
            <TextBlock DockPanel.Dock="Left" 
                       Text="يومية" 
                       Style="{StaticResource CaptionStyle}" />

            <Scenarios:NewBar HorizontalAlignment="Right" DockPanel.Dock="Right" />
            <Scenarios:SaveBar HorizontalAlignment="Right" />
        </DockPanel>

        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*" />
                <ColumnDefinition Width="1*" />
                <ColumnDefinition Width="1*" />
                <ColumnDefinition Width="1*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <HeaderedContentControl Grid.Row="0" Grid.Column="0" Header="رقم اليومية">
                <TextBox Height="30" 
                         VerticalContentAlignment="Center"
                         Text="{Binding Path=Entity.JournalID}"
                         IsReadOnly="True" />
            </HeaderedContentControl>

            <HeaderedContentControl Grid.Row="0" Grid.Column="1" Header="تاريخ اليومية">
                <DatePicker Height="30" 
                            VerticalContentAlignment="Center"
                            SelectedDate="{Binding Path=Entity.CreateDate, ValidatesOnDataErrors=True, NotifyOnValidationError=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
            </HeaderedContentControl>

            <HeaderedContentControl Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Header="ملاحظات">
                <TextBox Height="60" 
                         Text="{Binding Path=Entity.Notes, ValidatesOnDataErrors=True, NotifyOnValidationError=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                         Validation.ErrorTemplate="{StaticResource ValidationTemplate}" />
            </HeaderedContentControl>

            <DataGrid Name="dgEntries"
                      Grid.Row="2" 
                      Grid.Column="0"
                      Grid.ColumnSpan="4"
                      ItemsSource="{Binding Path=Entity.Entries}"
                      AutoGenerateColumns="False"
                      PreviewKeyDown="OnKeyDown"
                      SelectionUnit="CellOrRowHeader"
                      SelectionMode="Extended"
                      MinHeight="100"
                      SelectedCellsChanged="dgEntries_SelectedCellsChanged"
                      PreparingCellForEdit="dgEntries_PreparingCellForEdit">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Path=Debit, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                            Header="مدين"
                                            Width="15*" />

                    <DataGridTextColumn Binding="{Binding Path=Credit, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                            Header="دائن"
                                            Width="15*" />

                    <DataGridTemplateColumn x:Name="taskColumnFaster"
                                                Header="الحساب">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding Path=Account.AccountNumber}" />
                                    <TextBlock Text=" - " />
                                    <TextBlock Text="{Binding Path=Account.AccountName}" />
                                </StackPanel>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>

                        <DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <Grid FocusManager.FocusedElement="{Binding ElementName= taskCombo}">
                                    <ComboBox x:Name="taskCombo"
                                                  ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.TransactionalAccounts}"
                                                  DisplayMemberPath="AccountName"
                                                  SelectedValuePath="AccountID"
                                                  SelectedItem="{Binding Path=Account, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                                  IsHitTestVisible="True"
                                                  SelectionChanged="taskCombo_SelectionChanged"
                                                  PreviewKeyDown="taskCombo_KeyDown"
                                                  IsSynchronizedWithCurrentItem="False" />
                                </Grid>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellEditingTemplate>
                    </DataGridTemplateColumn>

                    <DataGridTextColumn Binding="{Binding Path=Description, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                            Header="الوصف"
                                            Width="50*" />
                </DataGrid.Columns>
            </DataGrid>

            <HeaderedContentControl Grid.Row="3"
                                    Grid.Column="0"
                                    Header="مجموع المدين">
                <TextBox Text="{Binding Path=Entity.TotalDebit, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                         IsReadOnly="True" />
            </HeaderedContentControl>

            <HeaderedContentControl Grid.Row="3"
                                    Grid.Column="1"
                                    Header="مجموع الدائن">
                <TextBox Text="{Binding Path=Entity.TotalCredit, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                         IsReadOnly="True" />
            </HeaderedContentControl>

            <HeaderedContentControl Grid.Row="3"
                                    Grid.Column="2"
                                    Header="الرصيد">
                <TextBox Text="{Binding Path=Entity.Balance, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                         IsReadOnly="True" />
            </HeaderedContentControl>

            <!--<HeaderedContentControl Header="مرحلة؟">
                <CheckBox IsChecked="{Binding Path=Entity.PostState}" />
            </HeaderedContentControl>-->

            <!--<StackPanel Grid.Row="10" Orientation="Vertical">
                <Border BorderBrush="Yellow"
                        BorderThickness="1">
                    <ListBox ItemsSource="{Binding Path=Entity.ValidationResults, UpdateSourceTrigger=PropertyChanged}"
                             MinHeight="100">
                        <ListBox.ItemTemplate>
                            <HierarchicalDataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding Path=ErrorMessage}" />
                                    <ListBox ItemsSource="{Binding Path=MemberNames}" />
                                </StackPanel>
                            </HierarchicalDataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </Border>
            </StackPanel>-->
            <!--<Label Content="إذا أردت إنشاء مجموعة جديدة\r\nقم بالضغط على زر جديد أولا."
                   VerticalContentAlignment="Center"
                   HorizontalContentAlignment="Center"
                   Background="White"
                   Opacity=".7"
                   Visibility="{Binding Path=Entity, Converter={StaticResource nullConverter}}" />-->
        </Grid>
    </Grid>
</UserControl>
